package classes;

import util.*;
import java.sql.*;

public class Register {
    
    DBUtil db = new DBUtil();
    public String message = "";
   
    public Register() {
    }
    
    public boolean processFormData(String fname, String lname, String email, String parentemail,
                           String schoolname, String location,String grade,
                           java.sql.Date dob, String iam13, String pwd) {
 
    	boolean success = true;
        String schooledinid = "";
        Debug.log("In processFormData:");
        db.connect();
        PreparedStatement pstmt = null;
        ResultSet generatedKeys = null;
        int keygen = 0;
        java.util.Random rdg = new java.util.Random();
        
        try {
          /*
           * Insert into USER_AUTH and get generated ID.
           */
          String sql = "INSERT INTO SCHOOLEDIN.USER_AUTH(USERNAME,PASSWORD,TYPE) VALUES(?,?,?)";	
          Debug.log("Exec: " + sql);    
          pstmt = db.conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
          pstmt.setString(1,email);
          pstmt.setString(2,pwd);
          pstmt.setString(3,"STUDENT");
          pstmt.execute();
            
          generatedKeys = pstmt.getGeneratedKeys();
          while(generatedKeys.next()) {
            keygen = generatedKeys.getInt(1);
            Debug.log("Keygen: " + keygen);
          }
          db.conn.commit();
          pstmt.close();
          pstmt = null;
          sql = null;
            
          /*
           * Insert into STUDENTS tables. 	
           */
          sql = "INSERT INTO SCHOOLEDIN.STUDENTS(" +
                           "SID,FNAME,LNAME,EMAIL,PARENT_EMAIL," +
                           "SCHOOL_NAME,LOCATION,GRADE,DOB," +
                           "ABOVE_13,SCHOOLEDINID) " +
                           "VALUES(?,?,?,?,?,?,?,?,?,?,?)";
          pstmt = db.conn.prepareStatement(sql);
          pstmt.setInt(1,keygen);
          pstmt.setString(2,fname);
          pstmt.setString(3,lname);
          pstmt.setString(4,email);
          pstmt.setString(5,parentemail);
          pstmt.setString(6,schoolname);
          pstmt.setString(7,location);
          pstmt.setInt(8,Integer.parseInt(grade));
          pstmt.setDate(9,dob);
         
          if(iam13 == null) {
            iam13 = "N";
          }
          else { iam13 = "Y"; }
            
          pstmt.setString(10,iam13);
          Integer r = rdg.nextInt(1000) + 1;
          schooledinid = fname.substring(0,1);
          schooledinid = schooledinid.concat(lname);
          schooledinid = schooledinid.concat(r.toString());
          pstmt.setString(11,schooledinid);
          Debug.log("Exec: " + sql);
          pstmt.execute();
          db.conn.commit();
        }
        catch(SQLException e) { 
          Debug.log("From ProcesFormDataX:");
          Debug.loge(e);
          message += e.getMessage();
          success = false;
        }
        finally {
          try {
            generatedKeys.close();
            pstmt.close();
            db.conn.commit();
            db.close();
          }
          catch(SQLException e) {
            Debug.log("From Finally Clause:");
            Debug.loge(e);
            success = false;
           }
        }
    return success;
    }
}
